package com.xmg.dataStructure.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 中缀表达式转后缀表达式
 * 使用后缀表达式实现计算器功能
 */
public class PolandNotation {
    public static void main(String[] args) {
        String expression = "1+((2+3)*4)-5";

        List<String> strings = toInfixExpressionList(expression);
        System.out.println("中缀表达式list"+strings);
        List<String> suffixExpressionList = parseSuffixExpressionList(strings);
        System.out.println("后缀表达式list"+suffixExpressionList);
        int result = caculate(suffixExpressionList);
        System.out.println("计算结果："+result);
    }

    //将中缀表达式转为对应的List
    public static List<String> toInfixExpressionList(String s){
        List<String> ls = new ArrayList<>();
        int i = 0;  //指针，用于遍历中缀表达式字符串
        String str; //对多位数的拼接
        char c;
        do {
            //如果c是非数字，直接加入到ls
            if((c = s.charAt(i))<48 || (c = s.charAt(i))>57){
                ls.add(""+c);
                i++;
            }else{//如果是一个数，考虑多位数的问题
                str = "";
                while (i<s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57){
                    str += c;
                    i++;
                }
                ls.add(str);
            }
        }while (i < s.length());
        return ls;
    }

    //将中缀表达式list转换为后缀表达式list
    public static List<String> parseSuffixExpressionList(List<String> ls){
        //定义两个栈 第二个栈没有pop操作，最后还需要逆序输出，所以此处直接使用list
        Stack<String> s1 = new Stack<>(); //符号栈
        List<String> s2 = new ArrayList<>();

        //遍历ls
        for (String item : ls) {
            //如果是一个数，就加入s2
            if(item.matches("\\d+")){
                s2.add(item);
            }else if(item.equals("(")){
                s1.push(item);
            }else if(item.equals(")")){
                while (!s1.peek().equals("(")){
                    s2.add(s1.pop());
                }
                s1.pop(); //将（弹出栈，消除一对括号
            }else{
                //当item的优先级小于等于s1栈顶的运算符，将s1栈顶的运算符弹出加入到s2中，再次转到与s1栈顶运算符相比较
                while (s1.size()!=0&&Operation.getValue(s1.peek()) >= Operation.getValue(item)){
                    s2.add(s1.pop());
                }
                //还需要将item压入栈
                s1.push(item);
            }
        }

        //将s1中剩余的运算符依次弹出加入到s2
        while (s1.size()!=0){
            s2.add(s1.pop());
        }
        return s2;
    }

    //计算器功能
    public static int caculate(List<String> suffixExpression){
        Stack<String> stack = new Stack<>();
        for (String item : suffixExpression) {
            if(item.matches("\\d+")){
                //判断为数字，直接入栈
                stack.push(item);
            }else{
                //符号，取出栈顶的两个数，进行操作
                int num2 = Integer.parseInt(stack.pop());
                int num1 = Integer.parseInt(stack.pop());
                int res = cal(num1, num2, item.charAt(0));
                stack.push(""+res);
            }
        }
        return Integer.parseInt(stack.pop());
    }

    //计算方法
    public static int cal(int num1,int num2,int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num1 - num2;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num1 / num2;
                break;
            default:
                break;
        }
        return res;
    }
}

class Operation{
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 1;
    private static int DIV = 1;

    public static int getValue(String operation){
        int result = 0;
        switch (operation){
            case "+":
                result = ADD;
                break;
            case "-":
                result = SUB;
                break;
            case "*":
                result = MUL;
                break;
            case "/":
                result = DIV;
                break;
            default:
                System.out.println("不存在该运算符");
                break;
        }
        return result;
    }
}